//
//  ChatTextCell.swift
//  EyeVisionForDoctors
//
//  Created by PMC on 2020/2/17.
//  Copyright © 2020 tao Tao. All rights reserved.
//

import UIKit

class ChatTextCell: ChatBaseCell {
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        bubbleView.addSubview(contentLabel)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func configure(with vm: ChatMsgViewModel, detailVm: AdvisoryDetailViewModel?) {
        super.configure(with: vm, detailVm: detailVm)
        contentLabel.textColor = vm.userType.textColor
        contentLabel.attributedText = NSAttributedString(string: vm.msgContent)
        
        // 设置泡泡
        bubbleView.image = vm.bubbleImage
        
        setupUILayout(with: vm)
    }
    
    private lazy var contentLabel: UILabel = {
        let contentL = UILabel()
        contentL.numberOfLines = 0
        contentL.textAlignment = .left
        contentL.font = .regular(16)
        contentL.sizeToFit()
        return contentL
    }()
    
    func setupUILayout(with vm: ChatMsgViewModel) {
        
        guard let vm = viewModel else { return }
        let calc_max = UIScreen.main.bounds.size.width - (kChatMargin + kChatAvatarWidth + kChatMargin + 17 + kChatMargin * 2)
        let width = calc_max > 240 ? 240 : calc_max
        
        let contentSize = contentLabel.sizeThatFits(CGSize(width: width, height: .greatestFiniteMagnitude))
        
        // 重新布局
        avatarView.snp.remakeConstraints { (make) in
            make.width.height.equalTo(kChatAvatarWidth)
            make.top.equalTo(sendTimeLabel.snp.bottom).offset(8)
        }
        bubbleView.snp.remakeConstraints { (make) in
            make.top.equalTo(avatarView)
            make.bottom.equalTo(contentLabel.snp.bottom).offset(16)
        }
        contentLabel.snp.remakeConstraints { (make) in
            make.height.equalTo(contentSize.height)
            make.width.equalTo(contentSize.width)
        }
        sendTimeLabel.snp.remakeConstraints { (make) in
            make.top.centerX.equalToSuperview()
        }
        errorView.snp.remakeConstraints { (make) in
            make.width.height.equalTo(16)
            make.centerY.equalTo(bubbleView)
        }
        activityView.snp.makeConstraints { (make) in
            make.width.height.equalTo(16)
            make.centerY.equalTo(bubbleView)
        }
        
        switch vm.userType {
        case .patient:
            avatarView.snp.makeConstraints { (make) in
                make.right.equalToSuperview().offset(-kChatMargin)
            }
            bubbleView.snp.makeConstraints { (make) in
                make.right.equalTo(avatarView.snp.left).offset(-kChatMargin)
                make.left.equalTo(contentLabel.snp.left).offset(-kChatMargin * 2)
            }
            contentLabel.snp.makeConstraints { (make) in
                make.top.equalTo(bubbleView.snp.top).offset(12)
                make.right.equalTo(bubbleView.snp.right).offset(-17)
            }
            errorView.snp.makeConstraints { (make) in
                make.right.equalTo(bubbleView.snp.left).offset(-kChatMargin)
            }
            activityView.snp.makeConstraints { (make) in
                make.right.equalTo(bubbleView.snp.left).offset(-kChatMargin)
            }
        case .doctor:
            avatarView.snp.makeConstraints { (make) in
                make.left.equalToSuperview().offset(kChatMargin)
            }
            bubbleView.snp.makeConstraints { (make) in
                make.left.equalTo(avatarView.snp.right).offset(kChatMargin)
                make.right.equalTo(contentLabel.snp.right).offset(kChatMargin * 2)
            }
            contentLabel.snp.makeConstraints { (make) in
                make.top.equalTo(bubbleView.snp.top).offset(12)
                make.left.equalTo(bubbleView.snp.left).offset(17)
            }
        }
    }
    
}
